package com.ks.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ks.entity.User;
import com.ks.service.UserService;
import com.ks.utils.Factory;

/**
 * Servlet implementation class CheckUserNameServlet
 */
@WebServlet("/check")
public class CheckUserNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService userServiceImpl = (UserService) Factory.getBean("userServiceImpl");

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckUserNameServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		User user = userServiceImpl.getUserById(username);
		PrintWriter out = response.getWriter();
		if (user != null) {
			out.print("该用户名已经注册！");
		} else {
			out.println("该用户名可以使用！");
		}
	}

}
